package com.zys.el.user.controller;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.zys.el.core.controller.BaseController;
import com.zys.el.core.po.ResponseBean;
import com.zys.el.user.config.po.Start;
import com.zys.el.user.config.po.SynthesizeResult;
import com.zys.el.user.po.Target;
import com.zys.el.user.po.TargetUser;
import com.zys.el.user.service.ITargetService;
import com.zys.el.user.service.Impl.TargetUserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/target")
public class TargetController extends BaseController<ITargetService, Target> {

    @Autowired
    private TargetUserServiceImpl targetUserService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PutMapping("/finish/{userId}")
    public ResponseBean finishTargetByUserId(@PathVariable Long userId) throws JsonProcessingException {
        ResponseBean rm = new ResponseBean();
        int b = service.finishTargetByUserId(userId);
//        System.out.println(b);
        if (b == -1) {
            rm.setModel(null);
            rm.setSuccess(false);
            rm.setMsg("不能解决原因，不能完成");
            rm.setCode(500);
        } else if (b == 2) {
            rm.setModel(null);
            rm.setSuccess(false);
            rm.setMsg("目标已完成，不可重复确定");
            rm.setCode(500);
        } else {
            rm.setModel(null);
            rm.setSuccess(true);
            rm.setMsg("成功确定完成");
            rm.setCode(200);
        }
        return rm;
    }

    @PutMapping("/like/{tid}")
    public ResponseBean addTargetLikes(@PathVariable Long tid) {
        ResponseBean rn = new ResponseBean();
        if (tid == null || tid.equals("")) {
            rn.setMsg("服务器出错，目标id未设置");
            rn.setCode(500);
            rn.setModel(null);
            rn.setSuccess(false);
            return rn;
        }
        Target target = service.getById(tid);
        if (target == null) {
            rn.setModel(null);
            rn.setMsg("服务器错误，不存在该目标");
            rn.setCode(500);
            rn.setSuccess(false);
            return rn;
        }
        target.setUpdateTime(new Date());
        target.setLike(target.getLike() + 1);
        boolean b = service.saveOrUpdate(target);
        if (b == true) {
            rn.setMsg("成功点赞");
            rn.setSuccess(true);
            rn.setModel(target);
            return rn;
        } else {
            rn.setMsg("服务器出现不可避免错误，稍后重试！！！");
            rn.setCode(500);
            rn.setModel(target);
            return rn;
        }

    }


    @ApiOperation(value = "完成度", notes = "获取目标完成度与各任务组的完成度")
    @GetMapping("/finished/{userId}")
    public ResponseBean getTargetFinishedProcess(@PathVariable Long userId) throws IOException {
        ResponseBean rn = new ResponseBean();
//        String o = (String) redisTemplate.opsForValue().get("FN" + userId);
//        System.out.println("===========" + o);
//        if (o == null) {
            TargetUser targetUser = targetUserService.selectTargetByUserId(userId);
            Long targetId = targetUser.getTargetId();
            Start start = service.finishedTargetProcess(userId, targetId);
            rn.setSuccess(true);
            rn.setCode(200);
            rn.setModel(start);
            rn.setMsg("该目标已经完成：" + start.getTargetProcess());
//            return rn;
//        }
//        Start jsonObject = JSON.parseObject(o, Start.class);
//        rn.setSuccess(true);
//        rn.setCode(200);
//        rn.setModel(jsonObject);
        rn.setMsg("ok");
        return rn;


    }
}
